<template>
  <!-- 患者列表，用于测试预约端的第三方跳转预约 -->
  <div class="ky-container">
    <el-card>
      <div>患者申请单列表，用于测试预约端的第三方跳转预约
        <el-tag style="margin-left: 10px;" v-for="(item, index) in pathOptions" :key="index" @click="setIpt(item.value)">{{item.label}}：{{item.value}}</el-tag>
      </div>
    </el-card>
    <el-row :gutter="20" class="ky-mt10">
      <el-col :span="10">
        <el-card
          style="max-height: calc(100vh - 100px);overflow-y: auto;"
        >
          <el-form
            :model="formdata"
            label-width="120px"
          >
            <!-- label-position="right" -->
            <el-form-item label="地址">
              <el-select class="roomhosp_select" v-model="formdata.path" placeholder="请选择">
                <el-option
                  v-for="(item,index) in pathOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
              <el-input v-if="formdata.path === '其它'" v-model="formdata.url" placeholder="第三方地址"></el-input>
            </el-form-item>
            <el-form-item label="医院">
              <el-select class="roomhosp_select" filterable v-model="formdata.hospCode" @change="hospChange" placeholder="请选择">
                <el-option
                  v-for="(item,index) in mx_hospitalList"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="操作用户编号">
              <el-input v-model="formdata.operateCode" placeholder="操作用户编号"></el-input>
            </el-form-item>
            <el-form-item label="操作用户">
              <el-input v-model="formdata.operateUser" placeholder="操作用户"></el-input>
            </el-form-item>
            <el-form-item label="部位">
              <el-input v-model="formdata.examClass" placeholder="部位"></el-input>
            </el-form-item>
            <el-form-item label="患者id">
              <el-input v-model="formdata.patId" placeholder="患者id"></el-input>
            </el-form-item>
            <el-form-item label="患者身份证号">
              <el-input v-model="formdata.idCard" placeholder="患者身份证号"></el-input>
            </el-form-item>
            <el-form-item label="患者手机号">
              <el-input v-model="formdata.tel" placeholder="患者手机号"></el-input>
            </el-form-item>
            <el-form-item label="其它参数">
              <el-input v-model="formdata.other" placeholder="其它参数"></el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="isEncrypt">加密</el-checkbox>
            </el-form-item>
            <el-form-item label="加密接口地址" v-if="isEncrypt">
              <el-input v-model="formdata.api" placeholder="加密接口地址"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :disabled="!formdata.patId" @click="onSubmit">预约</el-button>
              <el-button type="primary" :disabled="!formdata.operateUser || !formdata.operateCode || !formdata.hospCode" @click="onRoomSearch">HIS查看机房号源</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="14">
        <el-card
          style="max-height: calc(100vh - 150px);overflow-y: auto;"
        >
          <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column type="index" label="序号" width="55"></el-table-column>
            <el-table-column prop="id" label="患者id"></el-table-column>
            <el-table-column label="操作" width="180">
              <template slot-scope="scope">
                <el-button size="small" type="primary" @click="goRouterMobile(scope.row)">预约</el-button>
              </template>
            </el-table-column>
          </el-table>

        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
// import {
//   postMobileJwtSm2Encrypt
// } from '@/api/api'
const plArr = [
  { id: 'Y0080207' },
  { id: 'Y0080214' },
  { id: 'J0080079' },
  { id: 'Y0080199' },
  { id: 'Y0090003' },
  { id: 'J0090003' },
  { id: 'Pt0038612' },
  { id: 'Y0080223' },
  { id: '903004' },
  { id: '202009041510412' },
  { id: 'J0090002' },
  { id: 'j0090013' },
  { id: 'y0090025' },
  { id: 'y0090050' },
  { id: 'Y0090131' },
  { id: 'y0090094' },
  { id: 'Y0090114' },
  { id: 'Y0090038' },
  { id: '907003' },
  { id: 'j0090047' },
  { id: '917005' },
  { id: 'y0090090' },
  { id: '915006' },
  { id: 'J0090059' },
  { id: 'j0090059' },
  { id: 'J0090060' },
  { id: '914004' },
  { id: '522005' },
  { id: 'Y0090094' },
  { id: '906003' },
  { id: '202009151650512' },
  { id: '918003' },
  { id: '920003' },
  { id: 'J0090079' }
]
export default {
  data() {
    return {
      isEncrypt: true,
      hospitalCode: this.$store.state.user.hospitalCode,
      formdata: {
        hospCode: this.$store.state.user.hospitalCode,
        hospName: '',
        url: '',
        // path: 'http://192.168.0.91:9050/medicalTechnologyAppointment',
        path: 'http://192.168.0.91:9061/consultationLogin',
        patId: '',
        idCard: '', // 患者身份证
        tel: '', // 患者手机号
        operateCode: '1234',
        examClass: '小腿',
        operateUser: this.$store.state.user.userName,
        api: 'http://192.168.0.38:9217',
        other: '&ps=1&pw=0&edm=wo'
      },
      pathOptions: [
        {
          label: '移动端',
          // value: 'http://192.168.0.91:9050/medicalTechnologyAppointment'
          value: 'http://192.168.0.91:9061/consultationLogin'
        }, {
          label: 'PC端',
          value: 'http://192.168.0.91:9062/index.html'
        }, {
          label: '其它',
          value: '其它'
        }
      ],
      patListShow: false,
      tableData: []
    }
  },
  async created() {
    await this.$getHospitalList()
    this.patListShow = this.$route.query.pl
    if (this.patListShow) {
      this.tableData = JSON.parse(JSON.stringify(plArr))
    }
    this.$nextTick(() => {
      this.hospChange()
    })
  },
  methods: {
    onSubmit() {
      if (this.formdata.patId) {
        this.goRouterMobile({ id: this.formdata.patId })
      }
    },
    onRoomSearch() {
      if (this.formdata.hospCode && this.formdata.operateUser && this.formdata.operateCode) {
        this.openRoomUrl()
      }
    },
    goRouterMobile(row) {
      console.log(row)
      const params = {
        HospitalCode: this.formdata.hospCode,
        value: row.id
      }
      if (!this.isEncrypt) {
        this.openUrl(row.id)
        return
      }
      axios({
        method: 'post',
        baseURL: this.formdata.api,
        url: '/api/v1/mobileJwt/sm2Encrypt',
        headers: {},
        params: params
      }).then(response => {
        console.log(response)
        if (response.status === 200) {
          const res = response.data
          if (res && res.code === 10000) {
            let h = false
            this.tableData.forEach(item => {
              if (item.id === row.id) {
                h = true
              }
            })
            if (!h) {
              this.tableData.unshift({ id: row.id })
            }
            this.openUrl(res.content)
          }
        }
      })
    },
    openRoomUrl() {
      const urlPath = window.g.APPOINT_URL + '/indexAppoint.html'
      const _url = `${urlPath}?hospitalCode=${this.formdata.hospCode}&operateUser=${this.formdata.operateUser}&operateCode=${this.formdata.operateCode}`
      const newWin = window.open(_url, '_blank')
      newWin.focus()
    },
    openUrl(patientId) {
      const urlPath = this.formdata.path === '其它' ? this.formdata.url : this.formdata.path
      const _url = `${urlPath}?hospitalCode=${this.formdata.hospCode}&hospitalName=${this.formdata.hospName}&patientId=${patientId}&operateUser=${this.formdata.operateUser}&operateCode=${this.formdata.operateCode}&examClass=${this.formdata.examClass}&idCard=${this.formdata.idCard}&tel=${this.formdata.tel}${this.formdata.other}`
      console.log(_url)
      const newWin = window.open(_url, '_blank')
      newWin.focus()
    },
    hospChange() {
      if (this.formdata.hospCode) {
        const hosp = this.mx_hospitalList.find(item => item.code === this.formdata.hospCode)
        if (hosp) {
          this.formdata.hospName = hosp.name
        }
      } else {
        this.formdata.hospName = ''
      }
    },
    setIpt(val) {
      this.formdata.url = val
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
